package com.zziq.cloudpicturebank.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zziq.cloudpicturebank.annotation.AuthCheck;
import com.zziq.cloudpicturebank.common.BaseResponse;
import com.zziq.cloudpicturebank.common.DeleteRequest;
import com.zziq.cloudpicturebank.common.ResultUtils;
import com.zziq.cloudpicturebank.constant.UserConstant;
import com.zziq.cloudpicturebank.exception.BusinessException;
import com.zziq.cloudpicturebank.exception.ErrorCode;
import com.zziq.cloudpicturebank.exception.ThrowUtils;
import com.zziq.cloudpicturebank.model.dto.categorytag.CategoryTagAddRequest;
import com.zziq.cloudpicturebank.model.dto.categorytag.CategoryTagQueryRequest;
import com.zziq.cloudpicturebank.model.dto.categorytag.CategoryTagUpdateRequest;
import com.zziq.cloudpicturebank.model.dto.space.SpaceAddRequest;
import com.zziq.cloudpicturebank.model.dto.space.SpaceQueryRequest;
import com.zziq.cloudpicturebank.model.dto.space.SpaceUpdateRequest;
import com.zziq.cloudpicturebank.model.entity.CategoryTag;
import com.zziq.cloudpicturebank.model.entity.Space;
import com.zziq.cloudpicturebank.model.entity.User;
import com.zziq.cloudpicturebank.model.enums.CategoryTagEnum;
import com.zziq.cloudpicturebank.model.vo.picture.PictureTagCategory;
import com.zziq.cloudpicturebank.model.vo.user.UserVO;
import com.zziq.cloudpicturebank.service.CategoryTagService;
import com.zziq.cloudpicturebank.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CategoryTagController
 * @Description
 * @Created by ZZIQ
 **/
@Slf4j
@RestController
@RequestMapping("/categoryTag")
public class CategoryTagController {
    @Resource
    private CategoryTagService categoryTagService;

    @Resource
    private UserService userService;


    /**
     * 分页获取空间列表（仅管理员可用）
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<CategoryTag>> listCategoryTagByPage(
            @RequestBody CategoryTagQueryRequest categoryTagQueryRequest) {
        long current = categoryTagQueryRequest.getCurrent();
        long size = categoryTagQueryRequest.getPageSize();
        // 查询数据库
        Page<CategoryTag> categoryTagPage = categoryTagService.page(new Page<>(current, size),
                categoryTagService.getQueryWrapper(categoryTagQueryRequest));
        return ResultUtils.success(categoryTagPage);
    }

    /**
     * 获取封装 分类列表和标签列表
     *
     * @param request
     * @return
     */
    @GetMapping("/list/vo")
    public BaseResponse<PictureTagCategory> listPictureTagCategory(HttpServletRequest request) {
        List<String> categoryList = categoryTagService.lambdaQuery()
                .eq(CategoryTag::getType, CategoryTagEnum.CATEGORY.getValue())
                .list()
                .stream()
                .map(CategoryTag::getName)
                .collect(Collectors.toList());
        List<String> tagList = categoryTagService.lambdaQuery()
                .eq(CategoryTag::getType, CategoryTagEnum.TAG.getValue())
                .list()
                .stream()
                .map(CategoryTag::getName)
                .collect(Collectors.toList());
        PictureTagCategory pictureTagCategory = new PictureTagCategory();
        pictureTagCategory.setCategoryList(categoryList);
        pictureTagCategory.setTagList(tagList);
        return ResultUtils.success(pictureTagCategory);
    }

    /**
     * 新增标签/分类
     *
     * @param categoryTagAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addCategoryTag(@RequestBody CategoryTagAddRequest categoryTagAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(categoryTagAddRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NO_AUTH_ERROR);
        long newId = categoryTagService.addCategoryTag(categoryTagAddRequest, loginUser);
        return ResultUtils.success(newId);
    }

    /**
     * 删除分类标签
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteCategoryTag(@RequestBody DeleteRequest deleteRequest
            , HttpServletRequest request) {
        Long categoryTagId = deleteRequest.getId();
        //校验id
        if (categoryTagId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //获取登录用户
        User loginUser = userService.getLoginUser(request);
        // 判断是否存在
        CategoryTag categoryTag = categoryTagService.getById(categoryTagId);
        ThrowUtils.throwIf(categoryTag == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        categoryTagService.deleteCategoryTagById(categoryTag);
        return ResultUtils.success(true);
    }

    /**
     * 更新分类标签（仅管理员可用）
     * @param categoryTagUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateCategoryTag(@RequestBody CategoryTagUpdateRequest categoryTagUpdateRequest,
                                             HttpServletRequest request) {
        //校验参数
        if (categoryTagUpdateRequest == null || categoryTagUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        categoryTagService.updateCategoryTag(categoryTagUpdateRequest);
        return ResultUtils.success(true);
    }
}


























































